﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravelSite.Entities;
using Dapper;

namespace TravelSite.DAL.Repositories
{
    public class PaymentRepository : BaseRepository<Payment>
    {
        protected override string TableName
        {
            get { return "[Payment]"; }
        }

        public override IEnumerable<Payment> FindAll()
        {
            string strSQL = @"  select vo.PaymentId, vo.PaymentName,
                    convert(bit, case 
                                    when 1 = 0 then 0
                                else 1 end) as CanDelete
                    from [Payment] (nolock) as vo 
                    order by vo.PaymentName ";

            IEnumerable<Payment> result = Connection.Query<Payment>(strSQL);
            return result;
        }

        public override Payment Find(int id)
        {
            Payment result = Connection.Query<Payment>(
                @"  select vo.PaymentId, vo.PaymentName
                    from [Payment] (nolock) as vo
                    where vo.PaymentId = @PaymentId", new { PaymentId = id }).SingleOrDefault();
            return result;
        }

        public override void Insert(Payment item)
        {
            Connection.Execute(
                @"insert into [Payment] 
                (PaymentName)
                values (@PaymentName)", item);
        }

        public override void Update(Payment item)
        {
            Connection.Execute(
                @"update Payment
                    set PaymentName = @PaymentName
                    where (PaymentId = @PaymentId)", item);
        }

        public override void Delete(int id)
        {
            Connection.Execute(
                @"delete 
                    from Payment
                    where (PaymentId = @PaymentId)", new { PaymentId = id });
        }

    }
}
